﻿using System;
using System.Windows.Forms;
using KKdMainLib;
using KKdMainLib.IO;
using KKdFARC = KKdMainLib.FARC;

namespace PD_Tool
{
    public static class Program
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();

        [ThreadStatic] public static string function = "";

        [ThreadStatic] private static bool JSON = true;
        public static void Exit() => Environment.Exit(0);
        
        public static void ConsoleDesign(string text, params string[] args)
        {
            text = string.Format(text, args);
            string Text = "█                                                  █";
            Text = Text.Remove(3) + text + Text.Remove(0, text.Length + 3);
            Console.WriteLine(Text);
        }

        public static void ConsoleDesign(bool Fill)
        {
            if (Fill) Console.WriteLine("████████████████████████████████████████████████████");
            else      Console.WriteLine("█                                                  █");
        }

        private static string GetArgs(string name, bool And, params string[] ext)
        {
            int L = ext.Length;
            string Out = (And ? "|" : "") + name + " files (";
            for (int i = 0; i < L; i++) { Out += "*." + ext[i]; if (i + 1 < L) Out += ", "; }
            Out += ")|";
            for (int i = 0; i < L; i++) { Out += "*." + ext[i]; if (i + 1 < L) Out += ";" ; }
            return Out;
        }

        private static string GetArgs(string name, params string[] ext)
        {
            int L = ext.Length;
            string Out = name + " files (";
            for (int i = 0; i < L; i++) { Out += "*." + ext[i]; if (i + 1 < L) Out += ", "; }
            Out += ")|";
            for (int i = 0; i < L; i++) { Out += "*." + ext[i]; if (i + 1 < L) Out += ";" ; }
            return Out;
        }

        public static string Choose(int code, string filetype, out string[] FileNames)
        {
            string MsgPack = GetArgs("MessagePack", true, "mp"  );
            string JSON    = GetArgs("JSON"       , true, "json");
            string BIN     = GetArgs("BIN"        , true, "bin" );
            string WAV     = GetArgs("WAV"        , true, "wav" );

            FileNames = new string[0];
            if (code == 1)
            {
                string Filter = GetArgs("All;", false, "*");
                     if (filetype == "a3da") Filter = GetArgs("A3DA", "a3da", "farc", "json", "mp") +
                        GetArgs("A3DA", true, "a3da") +   GetArgs("FARC", true, "farc") + JSON + MsgPack;
                else if (filetype == "bin" ) Filter = GetArgs("BIN" , "bin", "json", "mp") +
                        BIN + JSON + MsgPack;
                else if (filetype == "blt" ) Filter = GetArgs("BLT" , "blt");
                else if (filetype == "bon" ) Filter = GetArgs("BON" , "bon", "bin", "json", "mp") +
                        GetArgs("BON", true, "bon") + BIN + JSON + MsgPack;
                else if (filetype == "cct" ) Filter = GetArgs("CCT" , "cct");
                else if (filetype == "databank") Filter = GetArgs("DAT", "dat", "json", "mp") +
                        GetArgs("DAT", true, "dat") + JSON + MsgPack;
                else if (filetype == "dex" ) Filter = GetArgs("DEX" , "dex", "bin", "json", "mp") +
                        GetArgs("DEX", true, "dex") + BIN + JSON + MsgPack;
                else if (filetype == "dft" ) Filter = GetArgs("DFT" , "dft");
                else if (filetype == "diva") Filter = GetArgs("DIVA", "diva", "wav") +
                        GetArgs("DIVA", true, "diva") + GetArgs("WAV", true, "wav");
                else if (filetype == "dsc" ) Filter = GetArgs("DSC" , "dsc", "json", "mp") +
                        GetArgs("DSC", true, "dsc") + JSON + MsgPack;
                else if (filetype == "dve" ) Filter = GetArgs("Particles", "farc");
                else if (filetype == "farc") Filter = "FARC Archives (*.farc)|*.farc";
                else if (filetype == "json") Filter = GetArgs("JSON", "json");
                else if (filetype == "mp"  ) Filter = GetArgs("MessagePack", "mp");
                else if (filetype == "lit")  Filter = GetArgs("LIT" , "lit");
                else if (filetype == "str" ) Filter = GetArgs("STR" , "str", "bin", "json", "mp") +
                        GetArgs("STR", true, "str") + BIN + JSON + MsgPack;
                else if (filetype == "vag" ) Filter = GetArgs("VAG" , "vag", "wav") +
                        GetArgs("VAG", true, "vag") + GetArgs("WAV", true, "wav");
                     
                using (OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Application.StartupPath,
                    Filter = Filter, Multiselect = true, Title = "Choose file(s) to open:" })
                    if (ofd.ShowDialog() == DialogResult.OK) FileNames = ofd.FileNames;
            }
            else if (code == 2)
            {
                string Return = "";
                using (OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Application.StartupPath,
                    ValidateNames = false, CheckFileExists = false, Filter = " | ", CheckPathExists = true,
                    Title = "Choose any file in folder:", FileName = "Folder Selection." })
                    if (ofd.ShowDialog() == DialogResult.OK) Return = Path.GetDirectoryName(ofd.FileName);
                return Return;
            }
            return "";
        }
    }
}